const utils = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //是否查询到数据
    hasData:true,    
    //是否隐藏‘全文’文字
    isHiddenMore: false,
    
    //是否显示隐藏评论
    isHiddenDiscuss: true,
    //是否有点赞或者评论
    isDiscuss: true,
    //评论输入框是否隐藏
    discussInputHidden: true,
    //分页
    pageIndex: 1,
    //每页数量
    pageSize: 5,
    //车圈动态
    dataList: [],
    //总数量
    total: 0,
    //更多
    hasMore: false,
    //评论要提交的数据
    comPostType: 0,
    comPostId: 0,
    comOwnerShopId: 0,
    tipText:'',
    //查询关键字
    searchKey:'',
    keyWord:'',
    commentInputFocus: false,
  },
  bindMainTap: function () {
    var that = this;
    console.log('652163');
    var dataList = that.data.dataList;
    var animation = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    dataList.forEach((item, index) => {
      animation.width(0).step();
      item.animationData = animation.export();
      item.showCommentBtn = false;
    });
    that.setData({
      dataList: dataList,
      discussInputHidden: true,
      commentInputFocus: false
    });
  },
  //打电话
  callPhone: function (e) {
    var that = this;
    utils.safeDoAction(function () {
      var index = e.currentTarget.dataset.index;
      var dataList = that.data.dataList;
      var oInfo = dataList[index];
      utils.postData(
        utils.apiUrl + "/message/AddPhoneCall",
        {
          OwnerShopID: oInfo.ShopID,
          PostType: oInfo.PostType,
          PostId: oInfo.PostID,
          CommentUserID: utils.getUserId(),
          FromType: 1
        },
        function () {
          wx.makePhoneCall({
            phoneNumber: oInfo.Mobile
          });
        }
      );
     } );
  },
  //评论点击切换
  discussTap: function (e) {
    var that = this;
    utils.safeDoAction(function () {   
      var index = e.currentTarget.dataset.index;
      var dataList = that.data.dataList;
      var isShowHid = dataList[index].showCommentBtn;
      //创建动画实例
      var animation = wx.createAnimation({
        duration: 150,
        timingFunction: 'linear',
      });
      that.animation = animation;
      dataList.forEach((item, index) => {
        animation.width(0).step();//step动画效果分步
        item.animationData = animation.export();//export方法可以将动画的数据传递给标签组件上的animation属性
        item.showCommentBtn = false;
      });

      dataList.forEach((item, index) => {
        item.showCommentBtn = false;
      }); 
      if (isShowHid == true) {
        animation.width("300rpx").step();
        dataList[index].animationData = animation.export();
        dataList[index].showCommentBtn = false;
        that.setData({
          dataList: dataList,
          discussInputHidden: true
        });
        setTimeout(function () {
          animation.width("0rpx").step()
          dataList[index].animationData = animation.export();
          this.setData({
            dataList: dataList
          })
        }.bind(that), 200); 
      } else {
        dataList[index].showCommentBtn = true;
        animation.width(0).step();
        dataList[index].animationData = animation.export();
        that.setData({
          dataList: dataList,
          discussInputHidden: true
        });
        setTimeout(function () {
          animation.width("300rpx").step()
          dataList[index].animationData = animation.export();
          this.setData({
            dataList: dataList
          })
        }.bind(that), 200);
      }
      that.setData({
        dataList: dataList,
        discussInputHidden: true
      })
    });
  },
  discussInputTap: function (e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    var postType = dataset.posttype;
    var postId = dataset.postid;
    var ownerShopId = dataset.ownershopid;
    var comIndexli = dataset.index;
    var dataList = that.data.dataList;
    dataList[comIndexli].showCommentBtn = false;
    var animation = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    animation.width(0).step();
    dataList[comIndexli].animationData = animation.export();
    that.setData({
      dataList: dataList,
      discussInputHidden: false,
      comPostType: postType,
      comPostId: postId,
      comOwnerShopId: ownerShopId,
      comIndex: comIndexli,
      commentInputFocus: true,
      isHiddenDiscuss: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
   // that.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      pageIndex: 1
    });
    that.getData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (!that.data.hasMore) {
      that.setData({
        tipText: "没有更多数据了"
      });
      return;
    }
    else {
      that.setData({
        tipText: "正在加载更多数据.."
      });
    }
    if (that.data.isLoadingMore) {
      return;
    }
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      isLoadingMore: true
    });
    utils.getData(
      utils.apiUrl + '/CarCircle/GetSearchListByCondition',
      {
        page: that.data.pageIndex,
        rows: that.data.pageSize,
        PostType: that.data.filterTypeIsActive,
        BizTypeID: that.data.filterServerTwo,
        LocationID: that.data.cityActive,
        GroupID: that.data.cityActive,
        ProvinceID: that.data.selProvinceId,
        Content: that.data.searchKey
      },
      function (response) {
        if (response.status == 0) {
          var tempList = that.data.dataList;
          var data = response.data;
          var result = data.items;
          var total = data.total;
          var listCombine = tempList.concat(result);
          var hasMore = false;
          if (listCombine.length < total) {
            hasMore = true;
          }
          that.setData({
            dataList: listCombine,
            isLoadingMore: false,
            hasMore: hasMore
          });
        }
      },
      function () { },
      function () {
        wx.hideLoading();
      }
    );
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getData: function (key) {
    var that = this;
    var hasData = true;
    utils.getData(
      utils.apiUrl + '/CarCircle/GetSearchListByCondition',
      {
        page: that.data.pageIndex,
        rows: that.data.pageSize,
        PostType: 0,
        BizTypeID: 0,
        LocationID: 0,
        GroupID: 0,
        ProvinceID: 0,
        Content: that.data.searchKey
      },
      function (response) {
        if (response.status == 0) {
          var data = response.data;
          var dataList = data.items;
          var total = data.total;
          var hasMore = false;
          if (dataList.length < total) {
            hasMore = true;
          }
          if(total==0)
          {
            hasData=false;
          }
          that.setData({
            dataList: dataList,
            total: total,
            hasMore: hasMore,
            hasData: hasData
          });
        }
      },
      function () { },
      function () {
        
      }
    );
  },
  bindFormSubmit: function (e) {
    var that = this;
    var formData = e.detail.value;
    var cIndex = that.data.comIndex;
    if (formData.CommentContent == '') {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      });
      return;
    }
    var data = {
      OwnerShopID: that.data.comOwnerShopId,
      PostType: that.data.comPostType,
      PostId: that.data.comPostId,
      CommentUserID: utils.getUserId(),
      CommentContent: formData.CommentContent
    }
    utils.postData(
      utils.apiUrl + '/Message/AddComment',
      data,
      function (response) {
        var data = response.data;
        var obj = that.data.dataList;
        if (response.status == 0) {
          obj[cIndex].CommentInfo.push(data);
          that.setData({
            isHiddenDiscuss: true,
            dataList: obj,
            inputTxt: '',
            discussInputHidden: true
          })
        }
        else {
          wx.showModal({
            title: '评论失败',
            content: response.message,
            showCancel: false,
          });
        }
      },
      {},
      function () {
        
      }
    )
    return;
  },
  /**
   * 点赞
   */
  catchTapBtnLike: function (e) {
    var that = this;
    var dataset = e.currentTarget.dataset;
    var index = dataset.index;
    var postType = dataset.posttype;
    var postId = dataset.postid;
    var ownerShopId = dataset.ownershopid;
    var commentUserId = utils.getUserId();
    var animation = wx.createAnimation({
      duration: 150,
      timingFunction: 'linear',
    });
    that.animation = animation;
    var data = {
      OwnerShopID: ownerShopId,
      PostType: postType,
      PostId: postId,
      CommentUserID: utils.getUserId()
    }
    utils.postData(
      utils.apiUrl + '/Message/AddLike',
      data,
      function (response) {
        var dataList = that.data.dataList;
        if (response.status == 0) {
          var data = response.data;
          dataList[index].LikeUser.push(data);
        }
        dataList.forEach((item, index) => {
          animation.width(0).step();
          item.animationData = animation.export();
          item.showCommentBtn = false;
        });
        dataList[index].showCommentBtn = false;
        that.setData({
          dataList: dataList
        })
      },
      {},
      function () {
        that.setData({
          isHiddenDiscuss: true
        })
      }
    )
  },
  /**   
      * 预览图片  
      */
  previewImage: function (e) {
    var that = this
    var current = e.currentTarget.dataset.img;
    var contentlistTem = that.data.dataList;
    var postid = e.currentTarget.dataset.postid;
    var postype = e.currentTarget.dataset.postype;

    var list = [];
    for (let i = 0; i < contentlistTem.length; i++) {
      if (contentlistTem[i].PostID == postid && contentlistTem[i].PostType == postype) {
        list = contentlistTem[i].PostImgList;
        break;
      }
    }
    if (list.length == 0) {
      list.push(current);
    }   
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: list // 需要预览的图片http链接列表  
    })
  },
  bindconfirm:function(e)
  {
    var that  = this;
    var key = e.detail.value;
    that.setData({
      pageIndex:1,
      searchKey:key
    })
    this.getData();
  },
  clearInput:function(e)
  {
    var that = this;
    var key = e.detail.value;
    that.setData({
      keyWord: ''
    })
  },
  bindJumpToUserShop: function (e) {
    var shopid = e.currentTarget.dataset.shopid;
    var url = '/pages/carcircle/othermy/othermy?shopid=' + shopid;
    utils.safeOpenUrl(url);
  }
})